package cn.hesion.leetcode;
/**
 * ClassName: SetZeroes <br/>
 * Description: <br/>
 * date: 2022/6/11 21:38<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

/**
 *
 * @author: hesion
 * @create: 2022-06-11 21:38
 **/
public class SetZeroes {
    public void setZeroes(int[][] matrix) {
        int n = matrix.length;
        int m = matrix[0].length;
        int i,j;

        for(i=0;i<n;++i){
            for(j=0;j<m;++j){
                if(matrix[i][j]==0){
                    int tmpI=i;
                    int tmpJ=j;
                    //列不变，切换行,向上切换
                    while(tmpI>=0){
                        matrix[tmpI][j]=matrix[tmpI][j]==0?0:999;
                        --tmpI;
                    }
                    tmpI=i;
                    //列不变，切换行,向下切换
                    while(tmpI<n){
                        matrix[tmpI][j]=matrix[tmpI][j]==0?0:999;
                        ++tmpI;
                    }
                    //行不变，切换列,向上切换
                    while(tmpJ>=0){
                        matrix[i][tmpJ]=matrix[i][tmpJ]==0?0:999;
                        --tmpJ;
                    }
                    tmpJ=j;
                    //行不变，切换列,向下切换
                    while(tmpJ<m){
                        matrix[i][tmpJ]=matrix[i][tmpJ]==0?0:999;
                        ++tmpJ;
                    }
                }
            }
        }

        for(i=0;i<n;++i){
            int tmpI=i;
            for(j=0;j<m;++j){
                if(matrix[i][j]==999){
                    matrix[i][j]=0;
                }
            }
        }
    }

    public static void main(String[] args) {
        new SetZeroes().setZeroes(new int[][]{
                new int[]{0,1,2,0},
                new int[]{3,4,5,2},
                new int[]{1,3,1,5}
        });
    }
}
